<template>
  <div class="user-info-container">
    <el-dropdown trigger="click" @command="handleCommand">
      <div class="user-info">
        <!-- 用户头像 -->
        <el-avatar :size="32" :src="props.userDetail.avatar" class="user-avatar">
          {{ props.userDetail.store_name?.charAt(0) }}
        </el-avatar>
        <!-- 用户名 -->
        <span class="user-name">{{ props.userDetail.store_name }}</span>
        <el-icon class="el-icon--right" style="color: white"><arrow-down /></el-icon>
      </div>

      <template #dropdown>
        <el-dropdown-menu>
          <!-- 个人中心 -->
          <el-dropdown-item command="profile" :icon="User">
            个人中心
          </el-dropdown-item>
          <!-- 数据统计 -->

          <el-dropdown-item command="stastic" :icon="PieChart">
            数据统计
          </el-dropdown-item>

          <!-- 退出登录 -->
          <el-dropdown-item command="logout" divided :icon="SwitchButton">
            退出登录
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
  <MyDialog v-model="showChooseImgDialog" title="用户信息" width="75%">
    <!-- 这里可以传入任何自定义内容 -->
    <UserProfile :user="props.userDetail"></UserProfile>
    <!-- 自定义底部 -->
    <template #footer>
      <el-button @click="showChooseImgDialog = false">关闭</el-button>
    </template>
  </MyDialog>
</template>

<script setup>
import { ref } from 'vue'
import { ArrowDown, User, SwitchButton, PieChart } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router';
import LocalStorageUtil from '../../utils/LocalStorageUtil';
import MyDialog from '../common/MyDialog.vue';
import UserProfile from './UserProfile.vue';
import utils from '../../utils/utils';

const router = useRouter()

// 用户信息（实际项目中可以从Vuex/Pinia获取）
const props = defineProps({
  userDetail: {
    type: Object,
    default: () => ({})
  }
})

const showChooseImgDialog = ref(false);

// 处理下拉菜单命令
const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      showChooseImgDialog.value = true;
      break;
    case 'stastic':
      router.push('/dataStatistics')
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 退出登录确认
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 实际项目中这里调用退出接口
    LocalStorageUtil.clearExcept(["username", "password"]);
    utils.globalCacheMenu = [];
    ElMessage.success('退出成功')
    router.push('/login')
  }).catch(() => {
    // 取消退出
  })
}
</script>

<style scoped>
.user-info-container {
  display: flex;
  align-items: center;
  height: 100%;
  padding-right: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s;
  padding: 5px 10px;
  border-radius: 4px;
}

/* .user-info:hover {
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  } */

.user-avatar {
  margin-right: 12px;
  background-color: var(--el-color-primary);
}

.user-name {
  font-size: 14px;
  margin-right: 5px;
  color: white;
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .user-name {
    display: none;
  }
}
</style>